<template>
  <div class="artical-item" :class="{ 'artical-item--search': type === 'search' }">
    <div class="artical-item--title" @click="jump">{{ item.title }}</div>
    <div class="artical-item--des">
      <div class="des-img" v-if="item.cover">
        <img :src="item.cover" alt="" />
      </div>
      <div class="des-content">
        {{ item.sub_description }}
      </div>
    </div>
    <div class="artical-item--bottom" v-if="type === 'list'">
      <div class="op-list">
        <el-button type="warning" size="mini" @click="handleLikeClick(item)"> <i class="el-icon-caret-top"></i>点赞（{{ item.like }}） </el-button>
        <el-button type="warning" size="mini" @click="handleHateClick">
          <i class="el-icon-caret-bottom"></i>
        </el-button>
      </div>

      <div class="time">{{ timestampToDate(item.create_time).substring(0, 10) }}</div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    item: {
      require: true
    },
    type: {
      default: 'list'
    }
  },

  methods: {
    jump() {
      this.$router.push({
        name: 'Artical',
        params: {
          id: this.item.uu_id
        }
      })
    },

    handleLikeClick(item) {
      this.$api.setBlogLike({ uuid: item.uu_id }).then((res) => {
        if (res.code === 1) {
          if (this.messageBox) {
            this.messageBox.close()
            this.messageBox = null
          }
          this.messageBox = this.$message.success('设置成功')

          item.like = parseInt(item.like) + 1
        }
      })
    },

    handleHateClick() {
      if (this.errorBox) {
        this.errorBox.close()
        this.errorBox = null
      }

      this.errorBox = this.$message.error('您忍心嘛！！')
    }
  }
}
</script>


<style scoped lang='less'>
.artical-item {
  padding: 30px 20px;
  border-bottom: 1px solid #eee;

  &.artical-item--search {
    box-shadow: 0 0 5px #e5e5e5;
    margin: 20px 10px;
  }

  .artical-item--title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 15px;
    cursor: pointer;
    color: #000;
    transition: 0.5s;

    &:hover {
      color: chocolate;
    }
  }

  .artical-item--des {
    font-size: 14px;
    line-height: 28px;
    display: flex;
    .des-img {
      margin-right: 20px;
      img {
        width: auto;
        height: 110px;
        border-radius: 5px;
      }
    }
  }
  .artical-item--bottom {
    margin-top: 15px;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;

    .time {
      margin-right: 10px;
      margin-bottom: 5px;
      font-size: 13px;
    }
  }
}
</style>